package com.hollycrm.hollysqm.analyzer.seg;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.hollycrm.hollysqm.analyzer.dic.Dictionary;

/**
 * 切词器默认实现
 * Created by zhaihw on 2016/1/28.
 */
public abstract class AbstractSegment implements Segment {
    /**
     * 加载用户词典
     *
     * @param nature 词性
     * @param word 词语
     */
    @Override
    public void loadUserWord(String nature,String word) {
        if(StringUtils.isEmpty(word))return;

        Dictionary.getUserDictWords().add(word);
    }

    /**
     * 加载用户词典
     *
     * @param nature 词性
     * @param words  词语
     */
    @Override
    public void loadUserWord(String nature, String... words) {
        if(words == null || words.length == 0)return;
        CollectionUtils.addAll(Dictionary.getUserDictWords(), words);
    }

    /**
     * 加载停用词
     *
     * @param stopWords 停用词
     * @return 加载成功数量
     */
    @Override
    public void loadStopWord(String... stopWords) {
        if(stopWords == null || stopWords.length == 0)return;
        CollectionUtils.addAll(Dictionary.getStopWords(),stopWords);
    }

//    /**
//     * 发现新词
//     *
//     * @param sentence 句子
//     * @return 词语
//     */
//    @Override
//    public String findNewWord(String ... sentence)throws Exception{
//        List<String> list = new ArrayList<>();
//        for(String s : sentence)
//            list.add(s);
//        String words  = NagaoAlgorithm.applyNagao(list, Dictionary.getUserDictWords(), Dictionary.getStopWords());
//        return words;
//    }
//
//    /**
//     * 发现新词
//     *
//     * @param path 文件路径
//     * @return 新词
//     */
//    @Override
//    public String findNewWordByFile(String path) throws Exception{
//        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
//        String line;
//        List<String> lines = new ArrayList<>();
//        while((line = br.readLine())!=null)
//            lines.add(line);
//        return NagaoAlgorithm.applyNagao(lines, Dictionary.getUserDictWords(), Dictionary.getStopWords());
//    }
}
